from django.db import models
from django.conf import settings

class Experiment(models.Model):
    """实验记录模型"""
    STATUS_CHOICES = (
        ('pending', '待开始'),
        ('in_progress', '进行中'),
        ('completed', '已完成'),
        ('failed', '失败'),
    )
    
    STEP_CHOICES = (
        (1, '定位螺丝'),
        (2, '扣住螺丝'),
        (3, '拧下螺丝'),
        (4, '放入盒子'),
        (5, '实验完成'),
    )
    
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='experiments', verbose_name='用户')
    start_time = models.DateTimeField(auto_now_add=True, verbose_name='开始时间')
    end_time = models.DateTimeField(blank=True, null=True, verbose_name='结束时间')
    status = models.CharField(max_length=15, choices=STATUS_CHOICES, default='pending', verbose_name='状态')
    current_step = models.PositiveSmallIntegerField(choices=STEP_CHOICES, default=1, verbose_name='当前步骤')
    success = models.BooleanField(default=False, verbose_name='是否成功')
    notes = models.TextField(blank=True, null=True, verbose_name='备注')
    
    class Meta:
        verbose_name = '实验记录'
        verbose_name_plural = '实验记录'
        ordering = ['-start_time']
    
    def __str__(self):
        return f"{self.user.username}的实验 - {self.get_status_display()}"


class ExperimentStep(models.Model):
    """实验步骤记录"""
    experiment = models.ForeignKey(Experiment, on_delete=models.CASCADE, related_name='steps', verbose_name='实验')
    step_number = models.PositiveSmallIntegerField(verbose_name='步骤编号')
    start_time = models.DateTimeField(auto_now_add=True, verbose_name='开始时间')
    end_time = models.DateTimeField(blank=True, null=True, verbose_name='结束时间')
    is_completed = models.BooleanField(default=False, verbose_name='是否完成')
    
    class Meta:
        verbose_name = '实验步骤'
        verbose_name_plural = '实验步骤'
        ordering = ['experiment', 'step_number']
    
    def __str__(self):
        return f"实验{self.experiment.id}的第{self.step_number}步"
